<?php
class Credit_cards extends MY_Controller 
{
	var $curl = 'credit_cards';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $submenu = "_menu";
	var $id_user = '';
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model(array('/public/credit_cards_model'));

		$this->validation->set_fields($this->credit_cards_model->fields);
		
		$this->data['Title'] = T_('Credit Cards');		

		$this->language = parent::getLang();

		$this->name = T_('Credit Cards');
	}
	
	function index()
	{
		$this->data['id_user'] = $this->auth->getID();
		$this->credit_cards_model->search($this->data);
		
		$this->renderLayout();
	}

	function create()
	{
		$this->data['id_user'] = $this->auth->getID();
		
		if (!empty($_POST))
		{
			$rules = array('card_type', 'card_number', 'month_expired', 'year_expired', 'card_id_number', 'first_name', 'last_name');
			$this->validation->set_rules($this->credit_cards_model->getRules($rules));
			
			if ($this->validation->run() === TRUE)
			{				
				
				$item['id_user'] 		= $this->auth->getID();
				$item['account_category']= ROLE_CONTRIBUTOR;
				$item['card_type'] 		= $this->validation->card_type;
				$item['card_number'] 	= $this->validation->card_number;
				$item['month_expired']	= $this->validation->month_expired;
				$item['year_expired']	= $this->validation->year_expired;
				$item['card_id_number'] = $this->validation->card_id_number;
				$item['first_name'] 	= $this->validation->first_name;
				$item['last_name']		= $this->validation->last_name;
				$item['is_default_card']= $this->validation->is_default_card;
				
       			if ($this->credit_cards_model->insert($item))
       			{
         			redirect($this->curl . '/credit_cards/'); 
       			}				
			}
		}
		
		$this->data['Title'] .= ' / ' . T_('New Card');
		
		$this->renderLayout();			
	}
	
	function edit($id = null)
	{
		$this->data['id_user'] = $this->auth->getID();
		$this->credit_cards_model->search($this->data);
		
		if (!empty($id))
		{
			$object = $this->credit_cards_model->get($id);
			
			if (!count($object))
			{
				$this->renderElement('query_no_object');
				return false; 
			}
			
			if (!count($_POST))
			{        
				//passing data
				$this->validation->id 				= $object['id'];
				$this->validation->card_type 		= $object['card_type']; 
				$this->validation->card_number 		= $object['card_number']; 
				$this->validation->month_expired 	= $object['month_expired'];
				$this->validation->year_expired 	= $object['year_expired'];
				$this->validation->card_id_number 	= $object['card_id_number'];  				
				$this->validation->first_name 		= $object['first_name'];    
				$this->validation->last_name 		= $object['last_name'];    
				$this->validation->is_default_card 	= $object['is_default_card'];
			}
			else
			{				
				$rules = array('card_type', 'card_number', 'month_expired', 'year_expired', 'card_id_number', 'first_name', 'last_name');
				$this->validation->set_rules($this->credit_cards_model->getRules($rules));
				
				if ($this->validation->run() === TRUE)
				{          						
					//prepare data
					$item = array();
					$item['id'] 			= $this->validation->id;
					$item['card_type'] 		= $this->validation->card_type;
					$item['card_number'] 	= $this->validation->card_number;
					$item['month_expired']	= $this->validation->month_expired;
					$item['year_expired']	= $this->validation->year_expired;
					$item['card_id_number'] = $this->validation->card_id_number;
					$item['first_name'] 	= $this->validation->first_name;
					$item['last_name']		= $this->validation->last_name;
					$item['is_default_card']= $this->validation->is_default_card;
										
					$this->credit_cards_model->update($item);

					redirect($this->curl . '/credit_cards/');
				}
			}	
		}
        	
		$this->data['Title'] .= ' / ' . T_('Edit Card');
		
		$this->renderLayout();
	}
		
	function deleteItems()
	{		
		$data = $this->credit_cards_model->delete($_REQUEST['items']);

		echo $data;
	}

	function delete($id, $id_contributor)
	{
		$data = $this->credit_cards_model->delete($id);
	
		if ($data)
		{
			redirect($this->curl . '/index/'. $id_contributor);	
		}
		else
		{
			redirect($this->curl . '/edit/'. $id);
		}
	}

	function details()
	{
		$id = $this->auth->getID();
		//does the object exist?
		if (!empty($id))
		{
			$object = $this->credit_cards_model->get($id);
			
			if (!count($object))
			{
				$this->renderElement('query_no_object');
				return false; 
			}
			
			if (!count($_POST))
			{
				//passing data
				$this->validation->user_ID 		= $object['user_ID'];
				$this->validation->user_login 	= $object['user_login']; 
				$this->validation->user_name 	= $object['user_name']; 
				$this->validation->user_email 	= $object['user_email'];
				$this->validation->user_phone 	= $object['user_phone'];
				$this->validation->user_password = $object['user_password'];							 
			}
			else
			{
				$rules = array('user_login', 'user_password', 'user_name', 'user_email', 'user_phone');
				$this->validation->set_rules($this->credit_cards_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					//prepare data
					$item['user_ID'] = $this->validation->user_ID;
					$item['user_login'] = $this->validation->user_login;
					$item['user_password'] = $this->validation->user_password;					
					$item['user_name'] = $this->validation->user_name;
					$item['user_email'] = $this->validation->user_email;
					$item['user_phone'] = $this->validation->user_phone;					
					
					$this->credit_cards_model->update($item);
					
					redirect($this->curl . '/index');
				}
			}	
		}
	
		$this->view 	= $this->curl . '/details';
		$this->renderLayout();
	}	
}
?>